package com.usian.service.impl;

import com.github.pagehelper.PageHelper;
import com.usian.JsonUtils;
import com.usian.mapper.SearchItemMapper;
import com.usian.pojo.SerachItem;
import com.usian.service.SearchService;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SearchServiceImpl implements SearchService {

    @Autowired
    private SearchItemMapper searchItemMapper;

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Value("${ES_INDEX_NAME}")
    private String ES_INDEX_NAME;

    @Value("${ES_TYPE_NAME}")
    private String ES_TYPE_NAME;


    @Override
    public boolean importAll() {
        try {
            //1：查数据库
            List<SerachItem> esDocumentList = searchItemMapper.getItemList();
            //mysql查询总条数
            int count = searchItemMapper.findCount();

            //2:建索引库
            if(!isExistsIndex()){
                createIndex();
            }
            //3：循环添加 bulk
            int page=1;
            while (true){
                /**分页每次导入一千条*/
                PageHelper.startPage(page,1000);
                //1、查询mysql中的商品信息
                if(esDocumentList==null || esDocumentList.size()==0){
                    break;
                }
                BulkRequest bulkRequest = new BulkRequest();
                for (int i = 0; i < esDocumentList.size(); i++) {
                    SerachItem searchItem =  esDocumentList.get(i);
                    //2、把商品信息添加到es中
                    bulkRequest.add(new IndexRequest(ES_INDEX_NAME, ES_TYPE_NAME).
                            source(JsonUtils.objectToJson(searchItem), XContentType.JSON));
                }
                restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
                page++;

                //避免死循环
                System.out.println("mysql总条数："+count+",已导入条数："+1000*page);
                if(count < 1000*page){
                    System.out.println("跳出循环");
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public List<SerachItem> list(String q, Long page, Long pageSize) {
        try {
            //1:es  api
            SearchRequest searchRequest =new SearchRequest(ES_INDEX_NAME);
            searchRequest.types(ES_TYPE_NAME);

            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.multiMatchQuery(q,"item_title",
                    "item_desc","item_sell_point","item_category_name"));

            //分页
            Long  from = (page - 1) * pageSize;
            searchSourceBuilder.from(from.intValue());
            searchSourceBuilder.size(pageSize.intValue());


            //封装查询条件
            searchRequest.source(searchSourceBuilder);
            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

            //遍历结果封装List<SerachItem>
            SearchHit[] searchHits = searchResponse.getHits().getHits();

            List<SerachItem> list = new ArrayList<>();
//                for(int i =0;i<searchHits.length;i++){
//                    SearchHit searchHit = searchHits[i];
//                    SerachItem serachItem = JsonUtils.jsonToPojo(searchHit.getSourceAsString(), SerachItem.class);
//                    list.add(serachItem);
//                }

            for(SearchHit s :searchHits){
                SerachItem serachItem = JsonUtils.jsonToPojo(s.getSourceAsString(), SerachItem.class);
                list.add(serachItem);
            }

           return  list;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return null;
    }

    @Override
    public int insertDocument(String msg){

        try {
            //1:msg  skuId   去mysql查询数据 格式成es库的那些字段
            SerachItem serachItem = searchItemMapper.getItemById(Long.parseLong(msg));

            //2:跟新es库
            IndexRequest indexRequest =new IndexRequest(ES_INDEX_NAME,ES_TYPE_NAME);
            indexRequest.source(JsonUtils.objectToJson(serachItem),XContentType.JSON);
            IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

            return indexResponse.getShardInfo().getFailed();
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * 索引库是否存在   true 存在  false 不存在
     * @return
     * @throws IOException
     */
    private boolean isExistsIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest();
        request.indices(ES_INDEX_NAME);
        return restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
    }

    /**
     * 创建索引库
     *
     * @return
     * @throws IOException
     */
    private boolean createIndex() throws IOException {
        //创建索引请求对象，并设置索引名称
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(ES_INDEX_NAME);
        //设置索引参数
        createIndexRequest.settings(Settings.builder().put("number_of_shards",2)
                .put("number_of_replicas",1));
        createIndexRequest.mapping(ES_TYPE_NAME, "{\n" +
                "  \"_source\": {\n" +
                "    \"excludes\": [\n" +
                "      \"item_desc\"\n" +
                "    ]\n" +
                "  },\n" +
                "  \"properties\": {\n" +
                "    \"item_title\": {\n" +
                "      \"type\": \"text\",\n" +
                "      \"analyzer\": \"ik_max_word\",\n" +
                "      \"search_analyzer\": \"ik_smart\"\n" +
                "    },\n" +
                "    \"item_sell_point\": {\n" +
                "      \"type\": \"text\",\n" +
                "      \"analyzer\": \"ik_max_word\",\n" +
                "      \"search_analyzer\": \"ik_smart\"\n" +
                "    },\n" +
                "    \"item_price\": {\n" +
                "      \"type\": \"float\"\n" +
                "    },\n" +
                "    \"item_image\": {\n" +
                "      \"type\": \"text\",\n" +
                "      \"index\": false\n" +
                "    },\n" +
                "    \"item_category_name\": {\n" +
                "      \"type\": \"keyword\"\n" +
                "    },\n" +
                "    \"item_desc\": {\n" +
                "      \"type\": \"text\",\n" +
                "      \"analyzer\": \"ik_max_word\",\n" +
                "      \"search_analyzer\": \"ik_smart\"\n" +
                "    }\n" +
                "  }\n" +
                "}", XContentType.JSON);
        //创建索引操作客户端
        IndicesClient indices = restHighLevelClient.indices();

        //创建响应对象
        CreateIndexResponse createIndexResponse =
                indices.create(createIndexRequest, RequestOptions.DEFAULT);
        //得到响应结果  true  成功   false 失败
        return createIndexResponse.isAcknowledged();
    }
}
